package cn.ljy.authority.service.impl;

import cn.ljy.authority.config.sms.SmsProperties;
import cn.ljy.authority.config.sms.SmsService;
import cn.ljy.authority.dao.mapper.SysPhoneVerificationCodeMapper;
import cn.ljy.authority.model.SysPhoneVerificationCode;
import cn.ljy.authority.model.query.SysPhoneVerificationCodeQuery;
import cn.ljy.authority.service.SysPhoneVerificationCodeService;
import cn.ljy.common.exception.model.MyServiceException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.Date;

@Service
//@Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT,timeout=36000,rollbackFor= MyRollBackException.class)
@Validated//表示开启sprint的校检框架，会自动扫描方法里的@Valid（@Valid注解一般写在接口即可）
@Slf4j
public class ISysPhoneVerificationCodeService extends IBaseService<SysPhoneVerificationCode, SysPhoneVerificationCodeQuery> implements SysPhoneVerificationCodeService {

    @Autowired
    private SysPhoneVerificationCodeMapper mapper;
    @Autowired
    private SmsProperties smsProperties;
    @Autowired
    private SmsService smsService;

    @Override
    protected void beforeSave(SysPhoneVerificationCode sysPhoneVerificationCode) {
        //1.限制同一个IP地址，规定时间内只能发送一次验证码
        SysPhoneVerificationCode sysPhoneVerificationCodeOfLast=mapper.getLatestByIp(sysPhoneVerificationCode.getIp());
        if(sysPhoneVerificationCodeOfLast!=null){
            long currentTime = System.currentTimeMillis();//当前时间
            long lastSendTime = sysPhoneVerificationCodeOfLast.getCreateTime().getTime();//前一条短信的发送时间
            long retryIntervalMillis = smsProperties.getIpRetryInterval() * 1000L;//重复发送时间间隔
            if ((currentTime - lastSendTime) < retryIntervalMillis) {
                throw new MyServiceException("同一个客户端" + sysPhoneVerificationCode.getIp() + "在" + smsProperties.getIpRetryInterval() + "秒内已经发送过验证码，请勿重复发送");
            }
        }
        //2.获取系统中已经发送短信的手机号码，判断有没有超过重复发送的时间限制
        sysPhoneVerificationCodeOfLast = mapper.getLatestByPhone(sysPhoneVerificationCode.getPhone());
        if(sysPhoneVerificationCodeOfLast!=null){
            long currentTime = System.currentTimeMillis();//当前时间
            long lastSendTime = sysPhoneVerificationCodeOfLast.getCreateTime().getTime();//前一条短信的发送时间
            long retryIntervalMillis = smsProperties.getRetryInterval() * 1000L;//重复发送时间间隔
            if ((currentTime - lastSendTime) < retryIntervalMillis) {
                throw new MyServiceException("手机号" + sysPhoneVerificationCode.getPhone() + "在" + smsProperties.getRetryInterval() + "秒内已经发送过验证码，请勿重复发送");
            }
        }
        //3.设置手机短信信息
        sysPhoneVerificationCode.setIsUsed(false);
        sysPhoneVerificationCode.setCreateTime(new Date());
        //计算出过期时间
        sysPhoneVerificationCode.setExpireTime(new Date(System.currentTimeMillis() + smsProperties.getExpireSeconds() * 1000));

    }
}
